/**
 * 解决MessagePlugin重复弹框的问题，只显示最后一次弹框提示
 */
import {
  MessageInstance,
  MessageOptions,
  MessagePlugin as MP,
  MessagePluginType,
  MessageThemeList
} from 'tdesign-vue-next';

let messageInstance: Promise<MessageInstance>;

const ResetMessage = async (
  theme: MessageThemeList,
  message: string | MessageOptions,
  duration?: number | undefined
) => {
  if (messageInstance) {
    await MP.close(messageInstance);
  }
  messageInstance = MP(theme, message, duration);
  return messageInstance;
};

const handler = (theme: MessageThemeList) => {
  return (message: string | MessageOptions, duration?: number | undefined) => {
    return ResetMessage(theme, message, duration);
  };
};

ResetMessage.error = handler('error');
ResetMessage.success = handler('success');
ResetMessage.info = handler('info');
ResetMessage.warning = handler('warning');
ResetMessage.question = handler('question');
ResetMessage.loading = handler('loading');

export const MessagePlugin = ResetMessage as MessagePluginType;
